package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void modify(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.Update(category);
        return;
    }

    @Override
    public PageResult PageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage() ,categoryPageQueryDTO.getPageSize());
        Page categoryList = (Page) categoryMapper.PageQuery(categoryPageQueryDTO);
        PageResult<Category> pageResult = new PageResult<>();
        pageResult.setRecords(categoryList.getResult());
        pageResult.setTotal(categoryList.getTotal());
        return pageResult;
    }

    @Override
    public void startOrStop(Integer status, long id) {
        categoryMapper.startOrStop(status,id);
        return;
    }

    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setCreateUser(BaseContext.getCurrentId());
        categoryMapper.add(category);
        return;
    }

    @Override
    public void Delete(long id) {
        categoryMapper.delete(id);
        return;
    }

    @Override
    public List<Category> QueryBYType(Integer type) {
        if(type == null){
            List<Category> all = categoryMapper.getAll();
            return all;
        }
        List<Category> categoryList = categoryMapper.getBYType(type);
        return categoryList;
    }





}
